package com.xx.eurekaclient;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import reactor.core.publisher.Flux;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

@Component
@Primary
public class MySwaggerResourcesProvider implements SwaggerResourcesProvider {

	public static final String API_URI = "/v2/api-docs";

	private RouteLocator routeLocator;

	@Autowired
	private  GatewayProperties gatewayProperties;
	@Autowired
	public MySwaggerResourcesProvider(RouteLocator routeLocator) {
		this.routeLocator = routeLocator;
	}

	@Override
	public List<SwaggerResource> get() {


		List<SwaggerResource> resources = new ArrayList<>();
		List<String> routeHosts = new ArrayList<>();
		List<RouteDefinition> routes = gatewayProperties.getRoutes();

		for(RouteDefinition routeDefinition:routes){
			List<PredicateDefinition> predicates = routeDefinition.getPredicates();
			for(PredicateDefinition predicateDefinition:predicates){
				if("path".equalsIgnoreCase(predicateDefinition.getName())){
					String s = predicateDefinition.getArgs()
							.get(NameUtils.GENERATED_NAME_PREFIX + "0");
					SwaggerResource swaggerResource1 = new SwaggerResource();
					swaggerResource1.setName(routeDefinition.getId());
					swaggerResource1.setLocation(s.replace("/**", API_URI));
					resources.add(swaggerResource1);
				}
			}

		}

		//gatewayProperties.getRoutes().stream().filter(routeDefinition -> routeDefinition.getId().equals("ruoyi-system"))
		//		.forEach(routeDefinition -> routeDefinition.getPredicates().stream()
		//				.filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
		//				.forEach(predicateDefinition -> resources
		//						.add(swaggerResource(routeDefinition.getId(), predicateDefinition.getArgs()
		//								.get(NameUtils.GENERATED_NAME_PREFIX + "0").replace("/**", API_URI)))));

		Flux<Route> routes1 = this.routeLocator.getRoutes();

		this.routeLocator.getRoutes().filter(route -> route.getUri().getHost() != null)
				.subscribe(route -> routeHosts.add(route.getUri().getHost()));
		Set<String> dealed = new HashSet<>();
		routeHosts.forEach(instance -> {
			String url = "/" + instance + API_URI;
			if (dealed.add(url)) {
				SwaggerResource swaggerResource = new SwaggerResource();
				swaggerResource.setUrl(url);
				swaggerResource.setName(instance);
				resources.add(swaggerResource);
			}
		});
		return resources;
	}

}